package com.pac.weixin.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.common.tools.StringTools;
import com.common.tools.TimeTools;
import com.common.util.ErrorNumber;
import com.common.util.LogicException;
import com.pac.weixin.WeiXinKeyContent;
import com.pac.weixin.bean.WxChatAutoBean;
import com.pac.weixin.dao.WxChatAutoDao;
import com.pac.weixin.service.WxChatAutoService;
import com.pac.weixin.service.WxChatService;

/**
 * 微信自动回复Service实现
 * 
 * @author lujun.chen
 * @version [版本号, 2015年3月11日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service(value = "wxChatAutoService")
public class WxChatAutoServiceImpl implements WxChatAutoService {

	private static Logger logger = Logger
			.getLogger(WxChatAutoServiceImpl.class);

	@Autowired
	private WxChatAutoDao wxChatAutoDao;

	@Autowired
	private WxChatService wxChatService;

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public String addWxChatAuto(WxChatAutoBean wxChatAutoBean) {
		if (null == wxChatAutoBean) {
			logger.error("添加微信自动聊天记录出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(wxChatAutoBean.getOpenId())) {
			logger.error("填加微信自动聊天记录出错，微信openId为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "微信OpenId为空");
		}

		if (StringTools.isNull(wxChatAutoBean.getUserId())) {
			logger.error("添加微信自动聊天记录出错，用户ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "用户ID为空");
		}

		if (StringTools.isNull(wxChatAutoBean.getSendDate())) {
			logger.error("添加微信自动聊天记录出错，自动发送时间为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "自动发送时间为空");
		}

		wxChatAutoBean.setCreateDate(TimeTools.now());
		wxChatAutoBean.setState(WeiXinKeyContent.WEIXIN_CHAT_STATE_READ_NO);

		return wxChatAutoDao.addWxChatAuto(wxChatAutoBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean updateWxChatAuto(WxChatAutoBean wxChatAutoBean) {
		if (null == wxChatAutoBean) {
			logger.error("更新微信自动回复内容出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		return wxChatAutoDao.updateWxChatAuto(wxChatAutoBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean delWxChatAuto(WxChatAutoBean wxChatAutoBean) {
		if (null == wxChatAutoBean) {
			logger.error("删除微信自动聊天记录出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(wxChatAutoBean.getId())) {
			logger.error("删除微信自动聊天记录出错，ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "ID为空");

		}
		return wxChatAutoDao.delWxChatAuto(wxChatAutoBean);
	}

	@Override
	public List<WxChatAutoBean> queryWxChatAuto(WxChatAutoBean wxChatAutoBean) {

		return wxChatAutoDao.queryWxChatAuto(wxChatAutoBean);
	}

}
